import Vue from 'vue'
import VueRouter from "vue-router"
import Login from "../views/login/index"
import LayOut from "../views/layout/LayOut"
import Home from "../views/home/index"
import Product from "../views/pm/product/index"
import AddProduct from "../views/pm/product/add"
import UpdateProduct from "../views/pm/product/update"
import ProductCategory from "../views/pm/productCategory/index"
import ProductCateAdd from "../views/pm/productCategory/add"
import ProductCateUpdate from "../views/pm/productCategory/update"
import Order from "../views/om/index"
import OrderSetting from "../views/om/setting"
import Admin from "../views/um/admin/index"
import Menu from "../views/um/menu/index"
import Role from "../views/um/role/index"
import AllocMenu from "../views/um/role/allocMenu"
import NotFound from "../views/NotFound"

Vue.use(VueRouter)

const routes = [
  { 
    path: "/login", 
    name: "login", 
    component: Login 
  },
  {
    path: "/", 
    name: "layOut", 
    component: LayOut,
    redirect:"/home",
    children:[
      {path: "home", name: "home", component: Home}
    ]
  },
  {
    path: "/pm", 
    component: LayOut,
    redirect:"/pm/product",
    children:[
      {path: "product", name: "product", component: Product},
      {path: "addProduct", name: "addProduct", component: AddProduct},
      {path: "updateProduct", name: "updateProduct", component: UpdateProduct},
      {path: "productCategory", name: "productCategory", component: ProductCategory},
      {path: "productCateAdd", name: "productCateAdd", component: ProductCateAdd},
      {path: "productCateUpdate", name: "productCateUpdate", component: ProductCateUpdate},
    ]
  },
  {
    path: "/om", 
    component: LayOut,
    redirect:"/om/order",
    children:[
      {path: "order", name: "order", component: Order},
      {path: "setting", name: "setting", component: OrderSetting},
    ]
  },
  {
    path: "/um", 
    component: LayOut,
    redirect:"/um/admin",
    children:[
      {path: "admin", name: "admin", component: Admin},
      {path: "role", name: "role", component: Role},
      {path: "menu", name: "menu", component: Menu},
      {path: "allocMenu", name: "allocMenu", component: AllocMenu},
    ]
  },
  {
    path:"*",
    name:"notFount",
    component:NotFound
  }
]

const router = new VueRouter({
  routes
})

// 全局守卫
router.beforeEach((to,from,next)=>{
  let token = window.sessionStorage.getItem("token");
  if(to.path === "/login"){
    // 在这里判断是否登录
    if(token){
      // 已登录
      return next("/")
    }else{
      // 没有登录
      return next();
    }
  }
  if(!token){
    return next("/login")
  }
  next();
})


// 下面的几行代码是直接从网上copy的  防止点击报错
const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return RouterPush.call(this, to).catch(err => err)
}
const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(to) {
  return RouterReplace.call(this, to).catch(err => err)
}

export default router




